import base64
import traceback

from backend.app.app.api.calculation.unit_calculate import welloprcost
from backend.app.app.db.session_public import session_pub
import backend.app.app.models.db_owi_public_models as md_pub
from backend.app.app.db.session_private import session_pri
import backend.app.app.models.db_owi_private_models as md_pri


# 获取开发单元年度油井措施经济评价结果表信息
def get_evaluate_result():
    try:
        evaluate_result_inf = session_pub.query(md_pub.TbYearDvpunitOilstimulateEvaluateResult.DvpUnit_Name,  # 开发单元
                                                md_pub.TbYearDvpunitOilstimulateEvaluateResult.Year,  # 年度
                                                md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimType_Name,  # 措施类别
                                                md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_WellNumber_Valid,
                                                # 有效井次
                                                md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_WellValidRatio,
                                                # 有效率
                                                md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_ValidPeriod_Average,
                                                # 平均有效期
                                                md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_IncreOil_Average,
                                                # 平均累计增油量
                                                md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_CommodityRatio,
                                                # 原油商品率
                                                md_pub.TbYearDvpunitOilstimulateEvaluateResult.Oil_Price,  # 原油价格
                                                md_pub.TbYearDvpunitOilstimulateEvaluateResult.OilSale_TotalTaxRatio,
                                                # 销售税费率
                                                md_pub.TbYearDvpunitOilstimulateEvaluateResult.Money_Discount,
                                                # 资金贴现率
                                                md_pub.TbYearDvpunitOilstimulateEvaluateResult.Oil_OprCost,  # 操作吨油成本
                                                md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimCost_Average,
                                                # 平均作业成本
                                                md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_WellOprCost_Average,
                                                # 平均增油操作成本
                                                md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_Total_TaxRatio_Average,
                                                # 平均销售总税费
                                                md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_Gross_SalesIncome_Average,
                                                # # 平均销售毛收入
                                                md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_Static_Gains_Average,
                                                # 平均静态收益
                                                md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_NPV_Average,
                                                # 平均财务净现值
                                                md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_ROI,  # 投资利润率
                                                md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_NPVR,
                                                # 财务净现值率
                                                md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_FIRR,
                                                # 财务内部收益率
                                                md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_IncreOil_Critical_Average,
                                                # 平均临界增油
                                                md_pub.TbYearDvpunitOilstimulateEvaluateResult.Remark  # 备注
                                                ).all()

        return evaluate_result_inf
    except:
        print('查找信息失败')
        return False


# 根据单元名称筛选信息 DvpUnit_Name
def get_by_name(dvunit_name):
    try:
        data1 = session_pub.query(
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.DvpUnit_Name,  # 开发单元
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Year,  # 年度
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimType_Name,  # 措施类别
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_WellNumber_Valid,
            # 有效井次
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_WellValidRatio,
            # 有效率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_ValidPeriod_Average,
            # 平均有效期
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_IncreOil_Average,
            # 平均累计增油量
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_CommodityRatio,
            # 原油商品率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Oil_Price,  # 原油价格
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.OilSale_TotalTaxRatio,
            # 销售税费率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Money_Discount,
            # 资金贴现率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Oil_OprCost,  # 操作吨油成本
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimCost_Average,
            # 平均作业成本
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_WellOprCost_Average,
            # 平均增油操作成本
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_Total_TaxRatio_Average,
            # 平均销售总税费
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_Gross_SalesIncome_Average,
            # # 平均销售毛收入
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_Static_Gains_Average,
            # 平均静态收益
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_NPV_Average,
            # 平均财务净现值
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_ROI,  # 投资利润率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_NPVR,
            # 财务净现值率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_FIRR,
            # 财务内部收益率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_IncreOil_Critical_Average,
            # 平均临界增
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Remark  # 备注
        ).filter(md_pub.TbYearDvpunitOilstimulateEvaluateResult.DvpUnit_Name == dvunit_name).all()
        print(data1)
        return data1
    except:
        print('查找信息失败')
        return False


# 根据措施类型筛选信息 StimType_Name
def get_by_type(stimtype_name):
    try:
        data2 = session_pub.query(
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.DvpUnit_Name,  # 开发单元
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Year,  # 年度
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimType_Name,  # 措施类别
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_WellNumber_Valid,
            # 有效井次
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_WellValidRatio,
            # 有效率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_ValidPeriod_Average,
            # 平均有效期
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_IncreOil_Average,
            # 平均累计增油量
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_CommodityRatio,
            # 原油商品率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Oil_Price,  # 原油价格
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.OilSale_TotalTaxRatio,
            # 销售税费率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Money_Discount,
            # 资金贴现率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Oil_OprCost,  # 操作吨油成本
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimCost_Average,
            # 平均作业成本
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_WellOprCost_Average,
            # 平均增油操作成本
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_Total_TaxRatio_Average,
            # 平均销售总税费
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_Gross_SalesIncome_Average,
            # # 平均销售毛收入
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_Static_Gains_Average,
            # 平均静态收益
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_NPV_Average,
            # 平均财务净现值
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_ROI,  # 投资利润率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_NPVR,
            # 财务净现值率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_FIRR,
            # 财务内部收益率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_IncreOil_Critical_Average,
            # 平均临界增
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Remark  # 备注
        ).filter(md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimType_Name == stimtype_name).all()
        print(data2)
        return data2
    except:
        print('查找信息失败')
        return False


# 根据起始时间筛选数据
def get_by_startyear(start_year):
    try:
        data3 = session_pub.query(
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.DvpUnit_Name,  # 开发单元
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Year,  # 年度
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimType_Name,  # 措施类别
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_WellNumber_Valid,
            # 有效井次
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_WellValidRatio,
            # 有效率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_ValidPeriod_Average,
            # 平均有效期
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_IncreOil_Average,
            # 平均累计增油量
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_CommodityRatio,
            # 原油商品率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Oil_Price,  # 原油价格
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.OilSale_TotalTaxRatio,
            # 销售税费率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Money_Discount,
            # 资金贴现率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Oil_OprCost,  # 操作吨油成本
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimCost_Average,
            # 平均作业成本
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_WellOprCost_Average,
            # 平均增油操作成本
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_Total_TaxRatio_Average,
            # 平均销售总税费
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_Gross_SalesIncome_Average,
            # # 平均销售毛收入
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_Static_Gains_Average,
            # 平均静态收益
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_NPV_Average,
            # 平均财务净现值
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_ROI,  # 投资利润率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_NPVR,
            # 财务净现值率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_FIRR,
            # 财务内部收益率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_IncreOil_Critical_Average,
            # 平均临界增
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Remark  # 备注
        ).filter(md_pub.TbYearDvpunitOilstimulateEvaluateResult.Year >= start_year).all()
        print(data3)
        return data3
    except:
        print('查找信息失败')
        return False


# 结束时间
def get_by_endyear(end_year):
    try:
        data4 = session_pub.query(
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.DvpUnit_Name,  # 开发单元
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Year,  # 年度
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimType_Name,  # 措施类别
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_WellNumber_Valid,
            # 有效井次
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_WellValidRatio,
            # 有效率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_ValidPeriod_Average,
            # 平均有效期
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_IncreOil_Average,
            # 平均累计增油量
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_CommodityRatio,
            # 原油商品率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Oil_Price,  # 原油价格
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.OilSale_TotalTaxRatio,
            # 销售税费率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Money_Discount,
            # 资金贴现率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Oil_OprCost,  # 操作吨油成本
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimCost_Average,
            # 平均作业成本
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_WellOprCost_Average,
            # 平均增油操作成本
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_Total_TaxRatio_Average,
            # 平均销售总税费
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_Gross_SalesIncome_Average,
            # # 平均销售毛收入
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_Static_Gains_Average,
            # 平均静态收益
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_NPV_Average,
            # 平均财务净现值
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_ROI,  # 投资利润率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_NPVR,
            # 财务净现值率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_FIRR,
            # 财务内部收益率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_IncreOil_Critical_Average,
            # 平均临界增
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Remark  # 备注
        ).filter(md_pub.TbYearDvpunitOilstimulateEvaluateResult.Year <= end_year).all()
        print(data4)
        return data4
    except:
        print('查找信息失败')
        return False


# 根据多个查询条件
def get_by_allcondition(dvunit_name, stimtype_name, start_year, end_year):
    try:
        data5 = session_pub.query(
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.DvpUnit_Name,  # 开发单元
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Year,  # 年度
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimType_Name,  # 措施类别
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_WellNumber_Valid,
            # 有效井次
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_WellValidRatio,
            # 有效率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_ValidPeriod_Average,
            # 平均有效期
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_IncreOil_Average,
            # 平均累计增油量
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_CommodityRatio,
            # 原油商品率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Oil_Price,  # 原油价格
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.OilSale_TotalTaxRatio,
            # 销售税费率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Money_Discount,
            # 资金贴现率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Oil_OprCost,  # 操作吨油成本
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimCost_Average,
            # 平均作业成本
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_WellOprCost_Average,
            # 平均增油操作成本
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_Total_TaxRatio_Average,
            # 平均销售总税费
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_Gross_SalesIncome_Average,
            # # 平均销售毛收入
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_Static_Gains_Average,
            # 平均静态收益
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_NPV_Average,
            # 平均财务净现值
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_ROI,  # 投资利润率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_NPVR,
            # 财务净现值率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_FIRR,
            # 财务内部收益率
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_IncreOil_Critical_Average,
            # 平均临界增
            md_pub.TbYearDvpunitOilstimulateEvaluateResult.Remark  # 备注
        ).filter(md_pub.TbYearDvpunitOilstimulateEvaluateResult.Year >= start_year,
                 md_pub.TbYearDvpunitOilstimulateEvaluateResult.Year <= end_year,
                 md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimType_Name == stimtype_name,
                 md_pub.TbYearDvpunitOilstimulateEvaluateResult.DvpUnit_Name == dvunit_name).all()
        print(data5)
        return data5
    except:
        print('查找信息失败')
        return False


# 把页面表格的信息存储进数据库
def table_store(dvpunit_name, year, stimtype_name, stim_wellnumber_valid, stim_wellvalidratio, stim_validperiod_average
                , stim_increoil_average, stimcost_average, oil_oprcost, oil_price, stimoil_commodityratio,
                oilsale_totaltaxratio,
                money_discount, remark, stimoil_welloprcost_average, stimoil_total_taxratio_average,
                stimoil_gross_salesincome_average, stimoil_static_gains_average, stimoil_npv_average, stimoil_roi,
                stimoil_npvr, stimoil_firr, stim_increoil_critical_average, stimoil_price_critical):
    try:
        store_data = md_pub.TbYearDvpunitOilstimulateEvaluateResult(
            DvpUnit_Name=dvpunit_name,  # 开发单元
            Year=year,
            StimType_Name=stimtype_name,
            Stim_WellNumber_Valid=stim_wellnumber_valid,
            Stim_WellValidRatio=stim_wellvalidratio,
            Stim_ValidPeriod_Average=stim_validperiod_average,
            Stim_IncreOil_Average=stim_increoil_average,
            StimCost_Average=stimcost_average,
            Oil_OprCost=oil_oprcost,
            Oil_Price=oil_price,
            StimOil_CommodityRatio=stimoil_commodityratio,
            OilSale_TotalTaxRatio=oilsale_totaltaxratio,
            Money_Discount=money_discount,
            Remark=remark,
            StimOil_WellOprCost_Average=stimoil_welloprcost_average,  # 平均增油操作成本
            StimOil_Total_TaxRatio_Average=stimoil_total_taxratio_average,  # 平均销售总税费
            StimOil_Gross_SalesIncome_Average=stimoil_gross_salesincome_average,  # 平均销售毛收入
            StimOil_Static_Gains_Average=stimoil_static_gains_average,  # 平均静态收益
            StimOil_NPV_Average=stimoil_npv_average,  # 平均财务净现值
            StimOil_ROI=stimoil_roi,  # 投资利润率
            StimOil_NPVR=stimoil_npvr,  # 财务净现值率
            StimOil_FIRR=stimoil_firr,  # 财务内部收益率
            Stim_IncreOil_Critical_Average=stim_increoil_critical_average,  # 平均临界增油量
            StimOil_Price_Critical=stimoil_price_critical)  # 增油经济临界油价
        session_pub.add(store_data)

        session_pub.commit()
        print('添加数据成功')
        session_pub.close()
        return True
    except:
        print('添加数据失败')
        return False


# 把页面表格的信息存储进private数据库
def table_store_private(user_id, proj_id, dvpunit_name, year, stimtype_name,
                        stim_wellnumber_valid, stim_wellvalidratio, stim_validperiod_average
                        , stim_increoil_average, stimcost_average, oil_oprcost, oil_price, stimoil_commodityratio,
                        oilsale_totaltaxratio,
                        money_discount, remark,
                        stimoil_welloprcost_average, stimoil_total_tax_average,
                        stimoil_gross_salesincome_average, stimoil_static_gains_average, stimoil_npv_average,
                        stimoil_roi,
                        stimoil_npvr, stimoil_firr, stim_increoil_critical_average, stimoil_price_critical):
    try:
        store_data1 = md_pri.TbUserprojDvpunitOilstimevalOut(
            User_ID=user_id,
            Proj_ID=proj_id,
            DvpUnit_Name=dvpunit_name,
            Year=year,
            StimType_Name=stimtype_name,
            Stim_WellNumber_Valid=stim_wellnumber_valid,
            Stim_WellValidRatio=stim_wellvalidratio,
            Stim_ValidPeriod_Average=stim_validperiod_average,
            Stim_IncreOil_Average=stim_increoil_average,
            StimOil_CommodityRatio=stimoil_commodityratio,
            Oil_Price=oil_price,
            OilSale_TotalTaxRatio=oilsale_totaltaxratio,
            Money_Discount=money_discount,
            Oil_OprCost=oil_oprcost,
            StimCost_Average=stimcost_average,
            StimOil_WellOprCost_Average=stimoil_welloprcost_average,
            StimOil_Total_Tax_Average=stimoil_total_tax_average,
            StimOil_Gross_SalesIncome_Average=stimoil_gross_salesincome_average,
            StimOil_Static_Gains_Average=stimoil_static_gains_average,
            StimOil_NPV_Average=stimoil_npv_average,
            StimOil_ROI=stimoil_roi,
            StimOil_NPVR=stimoil_npvr,
            StimOil_FIRR=stimoil_firr,
            Stim_IncreOil_Critical_Average=stim_increoil_critical_average,
            StimOil_Price_Critical=stimoil_price_critical,
            Remark=remark)
        session_pri.add(store_data1)
        session_pri.commit()
        print('添加数据成功')
        session_pri.close()
        return True
    except:
        print('添加数据失败')
        return False


# 将输入数据存储进 开发单元年度原油操作成本表 tb_year_dvpunit_oilprodcost
def oilprodcost_store(dvpunit_name, year, fuelcost, power_cost, fluiddispose_cost, oildisplaceagent_cost,
                      nglrecovery_cost, transp_cost,
                      welltest_cost, other_cost, depre_cost):
    try:
        data = md_pub.TbYearDvpunitOilprodcost(DvpUnit_Name=dvpunit_name,
                                               Year=year,
                                               FuelCost=fuelcost,
                                               Power_Cost=power_cost,
                                               FluidDispose_Cost=fluiddispose_cost,
                                               OilDisplaceAgent_Cost=oildisplaceagent_cost,
                                               NGLRecovery_Cost=nglrecovery_cost,
                                               Transp_Cost=transp_cost,
                                               WellTest_Cost=welltest_cost,
                                               Other_Cost=other_cost,
                                               Depre_Cost=depre_cost,
                                               Oil_OprCost=fuelcost + power_cost + fluiddispose_cost + oildisplaceagent_cost
                                                           + nglrecovery_cost + transp_cost +
                                                           welltest_cost + other_cost + depre_cost)
        session_pub.add(data)
        session_pub.commit()
        print('添加数据成功')
        session_pub.close()
        return True
    except:
        print('添加数据失败')
        return False


# 将输入数据存储进 开发单元年度原油销售税费率表 tb_year_dvpunit_oilsalecost_ratio
def oilsalecost_ratio_store(dvpunit_name, year, addedvalue_taxratio, resource_taxratio, eduadd_taxratio,
                            citydvpadd_taxrate, income_taxrate,
                            managecost_ratio):
    try:
        data = md_pub.TbYearDvpunitOilsalecostRatio(DvpUnit_Name=dvpunit_name,
                                                    Year=year,
                                                    AddedValue_TaxRatio=addedvalue_taxratio,
                                                    Resource_TaxRatio=resource_taxratio,
                                                    EduAdd_TaxRatio=eduadd_taxratio,
                                                    CityDvpAdd_TaxRate=citydvpadd_taxrate,
                                                    Income_TaxRate=income_taxrate,
                                                    ManageCost_Ratio=managecost_ratio,
                                                    OilSale_TotalTaxRatio=addedvalue_taxratio + resource_taxratio +
                                                                          eduadd_taxratio + citydvpadd_taxrate + income_taxrate + managecost_ratio)
        session_pub.add(data)
        session_pub.commit()
        print('添加数据成功')
        session_pub.close()
        return True
    except:
        print('添加数据失败')
        return False


# 从多表格中获取 开发单元年度油井措施经济评价结果表 所需数据
def get_alldata():
    # 开发单元年度油井措施效果记录表
    data1 = session_pub.query(md_pub.TbYearDvpunitOilstimResult.DvpUnit_Name,  # 开发单元
                              md_pub.TbYearDvpunitOilstimResult.Year,  # 年度
                              md_pub.TbYearDvpunitOilstimResult.StimType_Name,  # 措施类别
                              md_pub.TbYearDvpunitOilstimResult.Stim_WellNumber_Valid,  # 有效井次
                              md_pub.TbYearDvpunitOilstimResult.Stim_WellValidRatio,  # 有效率
                              md_pub.TbYearDvpunitOilstimResult.Stim_ValidPeriod_Average,  # 平均有效期
                              md_pub.TbYearDvpunitOilstimResult.Stim_IncreOil_Average  # 平均累计增油量
                              ).all()
    for i in range(len(data1)):
        for j in range(len(data1[0])):
            if data1[i][j] is None:
                data1[i][j] = ''
    # print(data1)

    # 开发单元年度油井措施费用表 tb_year_dvpunit_oilstim_cost
    data2 = []
    for i in range(len(data1)):
        temp = session_pub.query(md_pub.TbYearDvpunitOilstimCost.Stim_Cost). \
            filter(data1[i][0] == md_pub.TbYearDvpunitOilstimCost.DvpUnit_Name,
                   data1[i][1] == md_pub.TbYearDvpunitOilstimCost.Year).first()  # 作业费用
        if temp is None:
            temp = ''
        data2.append(temp)
    # print(data2)

    # 开发单元年度原油价格表
    data3 = []
    for i in range(len(data1)):
        temp1 = session_pub.query(md_pub.TbYearDvpunitOilprice.Oil_CommodityRatio  # 原油商品率
                                  ).filter(data1[i][0] == md_pub.TbYearDvpunitOilprice.DvpUnit_Name,
                                           data1[i][1] == md_pub.TbYearDvpunitOilprice.Year).first()
        if temp1 is None:
            a = ''
        else:
            a = temp1[0]

        temp2 = session_pub.query(md_pub.TbYearDvpunitOilprice.Oil_Price  # 原油价格
                                  ).filter(data1[i][0] == md_pub.TbYearDvpunitOilprice.DvpUnit_Name,
                                           data1[i][1] == md_pub.TbYearDvpunitOilprice.Year).first()
        if temp2 is None:
            b = ''
        else:
            b = temp2[0]
        data3.append([a, b])
    # print('data3', data3)

    # 开发单元年度原油操作成本表 tb_year_dvpunit_oilprodcost
    data4 = []
    for i in range(len(data1)):
        temp = session_pub.query(md_pub.TbYearDvpunitOilprodcost.Oil_OprCost).filter(
            data1[i][0] == md_pub.TbYearDvpunitOilprodcost.DvpUnit_Name,
            data1[i][1] == md_pub.TbYearDvpunitOilprodcost.Year).first()  # 操作吨油成本
        if temp is None:
            a = ''
        else:
            a = temp[0]
        data4.append(a)
    # print(data4)

    # 开发单元年度原油销售税费率表 tb_year_dvpunit_oilsalecost_ratio
    data5 = []
    for i in range(len(data1)):
        temp = session_pub.query(md_pub.TbYearDvpunitOilsalecostRatio.OilSale_TotalTaxRatio). \
            filter(data1[i][0] == md_pub.TbYearDvpunitOilsalecostRatio.DvpUnit_Name,
                   data1[i][1] == md_pub.TbYearDvpunitOilsalecostRatio.Year).first()  # 销售税费率
        if temp is None:
            a = ''
        else:
            a = temp[0]
        data5.append(a)
    # print(data5)

    # 开发单元年度核算资金折现率表 tb_year_dvpunit_discount
    data6 = []
    for i in range(len(data1)):
        temp = session_pub.query(md_pub.TbYearDvpunitDiscount.Money_Discount).filter(
            data1[i][0] == md_pub.TbYearDvpunitDiscount.DvpUnit_Name,
            data1[i][1] == md_pub.TbYearDvpunitDiscount.Year).first()  # 资金折现率
        if temp is None:
            a = ''
        else:
            a = temp[0]
        data6.append(a)
    # print(data6)

    all_data = []
    for i in range(len(data1)):
        all_data.append([])
        for j in range(13):
            all_data[i].append(0)

    for i in range(len(data1)):
        all_data[i][0] = data1[i][0]  # 开发单元
        all_data[i][1] = data1[i][1]  # 年度
        all_data[i][2] = data1[i][2]  # 措施类别
        all_data[i][3] = data1[i][3]  # 有效井次
        all_data[i][4] = data1[i][4]  # 有效率
        all_data[i][5] = data1[i][5]  # 平均有效期
        all_data[i][6] = data1[i][6]  # 平均累计增油量
        all_data[i][7] = data2[i]  # 作业费用
        all_data[i][8] = data3[i][0]  # 原油商品率
        all_data[i][9] = data3[i][1]  # 原油价格
        all_data[i][10] = data4[i]  # 操作吨油成本
        all_data[i][11] = data5[i]  # 销售税费率
        all_data[i][12] = data6[i]  # 资金折现率
    return all_data


# 存储用户关闭第一页面时的数据
def save_page1_data(user_id, proj_id, page_data=None, filter_data=None, set_info=None, picture1=None,
                    picture2=None, picture3=None, remake=None):
    try:
        # 删除符合user_id, proj_id的上一条数据
        session_pri.query(md_pri.TbUserprojPage1).filter(md_pri.TbUserprojPage1.User_ID == user_id and
                                                         md_pri.TbUserprojPage1.Proj_ID == proj_id).delete()
        data = md_pri.TbUserprojPage1(User_ID=user_id,
                                      Proj_ID=proj_id,
                                      Page_data=page_data,
                                      Fliter_data=filter_data,
                                      Set_info=set_info,
                                      Picture1=picture1,
                                      Picture2=picture2,
                                      Picture3=picture3,
                                      Ramake=remake)
        session_pri.add(data)
        session_pri.commit()
        print('添加数据成功')
        session_pri.close()
        return True
    except:
        print('添加数据失败')
        return False


# 存储用户关闭第二页面时的数据
def save_page2_data(user_id, proj_id, page_data=None, filter_data=None, set_info=None, picture1=None,
                    picture2=None, picture3=None, remake=None):
    try:
        # 删除符合user_id, proj_id的上一条数据
        session_pri.query(md_pri.TbUserprojPage2).filter(md_pri.TbUserprojPage2.User_ID == user_id and
                                                         md_pri.TbUserprojPage2.Proj_ID == proj_id).delete()
        data = md_pri.TbUserprojPage2(User_ID=user_id,
                                      Proj_ID=proj_id,
                                      Page_data=page_data,
                                      Fliter_data=filter_data,
                                      Set_info=set_info,
                                      Picture1=picture1,
                                      Picture2=picture2,
                                      Picture3=picture3,
                                      Ramake=remake)
        session_pri.add(data)
        session_pri.commit()
        print('添加数据成功')
        session_pri.close()
        return True
    except:
        print('添加数据失败')
        return False


# 存储用户关闭第三页面时的数据
def save_page3_data(user_id, proj_id, page_data=None, filter_data=None, set_info=None, picture1=None,
                    picture2=None, picture3=None, remake=None):
    try:
        # 删除符合user_id, proj_id的上一条数据
        session_pri.query(md_pri.TbUserprojPage3).filter(md_pri.TbUserprojPage3.User_ID == user_id and
                                                         md_pri.TbUserprojPage3.Proj_ID == proj_id).delete()
        data = md_pri.TbUserprojPage3(User_ID=user_id,
                                      Proj_ID=proj_id,
                                      Page_data=page_data,
                                      Fliter_data=filter_data,
                                      Set_info=set_info,
                                      Picture1=picture1,
                                      Picture2=picture2,
                                      Picture3=picture3,
                                      Ramake=remake)
        session_pri.add(data)
        session_pri.commit()
        print('添加数据成功')
        session_pri.close()
        return True
    except:
        print('添加数据失败')
        return False


# 返回第一页面存储的数据
def return_page1_data(user_id, proj_id):
    try:
        data = session_pri.query(md_pri.TbUserprojPage1.Page_data,
                                 md_pri.TbUserprojPage1.Fliter_data,
                                 md_pri.TbUserprojPage1.Set_info,
                                 md_pri.TbUserprojPage1.Picture1,
                                 md_pri.TbUserprojPage1.Picture2,
                                 md_pri.TbUserprojPage1.Picture3
                                 ).filter(md_pri.TbUserprojPage1.User_ID == user_id and
                                          md_pri.TbUserprojPage1.Proj_ID == proj_id).all()
        return data
    except:
        return False


# 返回第二页面存储的数据
def return_page2_data(user_id, proj_id):
    try:
        data = session_pri.query(md_pri.TbUserprojPage2.Page_data,
                                 md_pri.TbUserprojPage2.Fliter_data,
                                 md_pri.TbUserprojPage2.Set_info,
                                 md_pri.TbUserprojPage2.Picture1,
                                 md_pri.TbUserprojPage2.Picture2,
                                 md_pri.TbUserprojPage2.Picture3
                                 ).filter(md_pri.TbUserprojPage2.User_ID == user_id and
                                          md_pri.TbUserprojPage2.Proj_ID == proj_id).all()
        return data
    except:
        return False


# 返回第三页面存储的数据
def return_page3_data(user_id, proj_id):
    try:
        data = session_pri.query(md_pri.TbUserprojPage3.Page_data,
                                 md_pri.TbUserprojPage3.Fliter_data,
                                 md_pri.TbUserprojPage3.Set_info,
                                 md_pri.TbUserprojPage3.Picture1,
                                 md_pri.TbUserprojPage3.Picture2,
                                 md_pri.TbUserprojPage3.Picture3
                                 ).filter(md_pri.TbUserprojPage3.User_ID == user_id and
                                          md_pri.TbUserprojPage3.Proj_ID == proj_id).all()
        return data
    except:
        return False


# 返回用户工程单元经济评价计算功能输出结果表 tb_userproj_dvpunit_oilstimeval_out
def return_tb_userproj_dvpunit_oilstimeval_out():
    try:
        data = session_pri.query(
            md_pri.TbUserprojDvpunitOilstimevalOut.User_ID,
            md_pri.TbUserprojDvpunitOilstimevalOut.Proj_ID,
            md_pri.TbUserprojDvpunitOilstimevalOut.DvpUnit_Name,
            md_pri.TbUserprojDvpunitOilstimevalOut.Year,
            md_pri.TbUserprojDvpunitOilstimevalOut.StimType_Name,
            md_pri.TbUserprojDvpunitOilstimevalOut.Stim_WellNumber_Valid,
            md_pri.TbUserprojDvpunitOilstimevalOut.Stim_WellValidRatio,
            md_pri.TbUserprojDvpunitOilstimevalOut.Stim_ValidPeriod_Average,
            md_pri.TbUserprojDvpunitOilstimevalOut.Stim_IncreOil_Average,
            md_pri.TbUserprojDvpunitOilstimevalOut.StimOil_CommodityRatio,
            md_pri.TbUserprojDvpunitOilstimevalOut.Oil_Price,
            md_pri.TbUserprojDvpunitOilstimevalOut.OilSale_TotalTaxRatio,
            md_pri.TbUserprojDvpunitOilstimevalOut.Money_Discount,
            md_pri.TbUserprojDvpunitOilstimevalOut.Oil_OprCost,
            md_pri.TbUserprojDvpunitOilstimevalOut.StimCost_Average,
            md_pri.TbUserprojDvpunitOilstimevalOut.StimOil_WellOprCost_Average,
            md_pri.TbUserprojDvpunitOilstimevalOut.StimOil_Total_Tax_Average,
            md_pri.TbUserprojDvpunitOilstimevalOut.StimOil_Gross_SalesIncome_Average,
            md_pri.TbUserprojDvpunitOilstimevalOut.StimOil_Static_Gains_Average,
            md_pri.TbUserprojDvpunitOilstimevalOut.StimOil_NPV_Average,
            md_pri.TbUserprojDvpunitOilstimevalOut.StimOil_ROI,
            md_pri.TbUserprojDvpunitOilstimevalOut.StimOil_NPVR,
            md_pri.TbUserprojDvpunitOilstimevalOut.StimOil_FIRR,
            md_pri.TbUserprojDvpunitOilstimevalOut.Stim_IncreOil_Critical_Average,
            md_pri.TbUserprojDvpunitOilstimevalOut.StimOil_Price_Critical,
            md_pri.TbUserprojDvpunitOilstimevalOut.Remark
        ).all()
        print(data)
        return data
    except:
        return False


# 返回用户工程单元经济评价计算功能输出结果表筛选信息 tb_userproj_dvpunit_oilstimeval_out
def return_tb_userproj_dvpunit_oilstimeval_out_byfilter(user_id, proj_id):
    try:
        data = session_pri.query(
            md_pri.TbUserprojDvpunitOilstimevalOut.DvpUnit_Name,
            md_pri.TbUserprojDvpunitOilstimevalOut.Year,
            md_pri.TbUserprojDvpunitOilstimevalOut.StimType_Name,
            md_pri.TbUserprojDvpunitOilstimevalOut.Stim_WellNumber_Valid,
            md_pri.TbUserprojDvpunitOilstimevalOut.Stim_WellValidRatio,
            md_pri.TbUserprojDvpunitOilstimevalOut.Stim_ValidPeriod_Average,
            md_pri.TbUserprojDvpunitOilstimevalOut.Stim_IncreOil_Average,
            md_pri.TbUserprojDvpunitOilstimevalOut.StimOil_CommodityRatio,
            md_pri.TbUserprojDvpunitOilstimevalOut.Oil_Price,
            md_pri.TbUserprojDvpunitOilstimevalOut.OilSale_TotalTaxRatio,
            md_pri.TbUserprojDvpunitOilstimevalOut.Money_Discount,
            md_pri.TbUserprojDvpunitOilstimevalOut.Oil_OprCost,
            md_pri.TbUserprojDvpunitOilstimevalOut.StimCost_Average,
            md_pri.TbUserprojDvpunitOilstimevalOut.StimOil_WellOprCost_Average,
            md_pri.TbUserprojDvpunitOilstimevalOut.StimOil_Total_Tax_Average,
            md_pri.TbUserprojDvpunitOilstimevalOut.StimOil_Gross_SalesIncome_Average,
            md_pri.TbUserprojDvpunitOilstimevalOut.StimOil_Static_Gains_Average,
            md_pri.TbUserprojDvpunitOilstimevalOut.StimOil_NPV_Average,
            md_pri.TbUserprojDvpunitOilstimevalOut.StimOil_ROI,
            md_pri.TbUserprojDvpunitOilstimevalOut.StimOil_NPVR,
            md_pri.TbUserprojDvpunitOilstimevalOut.StimOil_FIRR,
            md_pri.TbUserprojDvpunitOilstimevalOut.Stim_IncreOil_Critical_Average,
            md_pri.TbUserprojDvpunitOilstimevalOut.StimOil_Price_Critical,
            md_pri.TbUserprojDvpunitOilstimevalOut.Remark
        ).filter(md_pri.TbUserprojDvpunitOilstimevalOut.User_ID == user_id and
                 md_pri.TbUserprojDvpunitOilstimevalOut.Proj_ID == proj_id).all()
        print(data)
        return data
    except:
        return False


# 存储数据进用户工程单元经济评价计算功能输入数据表 tb_userproj_dvpunit_oilstimeval_input
def save_data_in_input(user_id, proj_id, dvpunit_name=None, year=None, stimtype_name=None, stim_wellnumber_valid=None,
                       stim_wellvalidratio=None, stim_validperiod_average=None, stim_increoil_average=None,
                       stimoil_commodityratio=None, oil_price=None, oilsale_totaltaxratio=None,
                       money_discount=None, oil_oprcost=None, stimcost_average=None, remark=None):
    try:
        session_pri.query(md_pri.TbUserprojDvpunitOilstimevalInput).filter(
            md_pri.TbUserprojDvpunitOilstimevalInput.User_ID == user_id and
            md_pri.TbUserprojDvpunitOilstimevalInput.Proj_ID == proj_id and
            md_pri.TbUserprojDvpunitOilstimevalInput.DvpUnit_Name == dvpunit_name and
            md_pri.TbUserprojDvpunitOilstimevalInput.Year == year).delete()
        data = md_pri.TbUserprojDvpunitOilstimevalInput(User_ID=user_id,
                                                        Proj_ID=proj_id,
                                                        DvpUnit_Name=dvpunit_name,
                                                        Year=year,
                                                        StimType_Name=stimtype_name,
                                                        Stim_WellNumber_Valid=stim_wellnumber_valid,
                                                        Stim_WellValidRatio=stim_wellvalidratio,
                                                        Stim_ValidPeriod_Average=stim_validperiod_average,
                                                        Stim_IncreOil_Average=stim_increoil_average,
                                                        StimOil_CommodityRatio=stimoil_commodityratio,
                                                        Oil_Price=oil_price,
                                                        OilSale_TotalTaxRatio=oilsale_totaltaxratio,
                                                        Money_Discount=money_discount,
                                                        Oil_OprCost=oil_oprcost,
                                                        StimCost_Average=stimcost_average,
                                                        Remark=remark)
        session_pri.add(data)
        session_pri.commit()
        print('添加数据成功')
        session_pri.close()
        return True

    except:
        print('添加数据失败')
        return False


# 存储计算表格数据恢复表  tb_userproj_oilstimeval
def save_userproj_oilstimeval(user_id, proj_id, input_data, output_data, save_data):
    try:
        is_exis=session_pri.query(md_pri.TbUserprojOilstimeval).filter(
            md_pri.TbUserprojOilstimeval.User_ID==user_id,
            md_pri.TbUserprojOilstimeval.Proj_ID==proj_id
        ).first()
        if is_exis:
            session_pri.query(md_pri.TbUserprojOilstimeval).filter(
                md_pri.TbUserprojOilstimeval.User_ID == user_id,
                md_pri.TbUserprojOilstimeval.Proj_ID == proj_id
            ).update({
                'Data' : save_data
            })
        else:
            session_pri.add(md_pri.TbUserprojOilstimeval(
                User_ID=user_id,Proj_ID=proj_id,Data=save_data
            ))
        session_pri.commit()

        input_data_db = session_pri.query(md_pri.TbUserprojDvpunitOilstimevalInput).filter(
            md_pri.TbUserprojDvpunitOilstimevalInput.User_ID == user_id,
            md_pri.TbUserprojDvpunitOilstimevalInput.Proj_ID == proj_id
        ).first()
        if input_data_db:
            session_pri.query(md_pri.TbUserprojDvpunitOilstimevalInput).filter(
                md_pri.TbUserprojDvpunitOilstimevalInput.User_ID == user_id,
                md_pri.TbUserprojDvpunitOilstimevalInput.Proj_ID == proj_id
            ).update({
                'InputData' : input_data,
            })
        else:
            session_pri.add(
                md_pri.TbUserprojDvpunitOilstimevalInput(
                    User_ID = user_id,
                    Proj_ID = proj_id,
                    InputData=input_data
                )
            )
        session_pri.commit()

        output_data_db = session_pri.query(md_pri.TbUserprojDvpunitOilstimevalOut).filter(
            md_pri.TbUserprojDvpunitOilstimevalOut.User_ID == user_id,
            md_pri.TbUserprojDvpunitOilstimevalOut.Proj_ID == proj_id
        ).first()
        if output_data_db:
            session_pri.query(md_pri.TbUserprojDvpunitOilstimevalOut).filter(
                md_pri.TbUserprojDvpunitOilstimevalOut.User_ID == user_id,
                md_pri.TbUserprojDvpunitOilstimevalOut.Proj_ID == proj_id
            ).update({
                'OutputData': output_data
            })
        else:
            session_pri.add(
                md_pri.TbUserprojDvpunitOilstimevalOut(
                    User_ID = user_id,
                    Proj_ID = proj_id,
                    OutputData = output_data
                )
            )
        session_pri.commit()
        return True
    except:
        session_pri.rollback()
        print(traceback.print_exc())
        return False


# 返回存储计算表格数据恢复表
def return_userproj_oilstimeval(user_id, proj_id):
    try:
        read_data=session_pri.query(
            md_pri.TbUserprojOilstimeval.Data
        ).filter(
            md_pri.TbUserprojOilstimeval.User_ID==user_id,
            md_pri.TbUserprojOilstimeval.Proj_ID==proj_id,
        ).first()
        if read_data:
            read_data=read_data[0]
        input_result=session_pri.query(
            md_pri.TbUserprojDvpunitOilstimevalInput.InputData
            ).filter(
            md_pri.TbUserprojDvpunitOilstimevalInput.User_ID==user_id,
            md_pri.TbUserprojDvpunitOilstimevalInput.Proj_ID==proj_id
        ).first()
        if input_result:
            input_result=input_result[0]

        output_result = session_pri.query(
            md_pri.TbUserprojDvpunitOilstimevalOut.OutputData
            ).filter(
            md_pri.TbUserprojDvpunitOilstimevalOut.User_ID == user_id,
            md_pri.TbUserprojDvpunitOilstimevalOut.Proj_ID == proj_id,
        ).first()
        if output_result:
            output_result = output_result[0]
        return {"Data":read_data,"Input_data":input_result,"Output_data":output_result}
    except Exception:
        print(traceback.print_exc())
        return False



# 存储用户曲线图数据恢复表  TbUserprojCurveDatum
def save_userproj_curve_data(user_id, proj_id, curve_data):
    try:
        session_pri.query(md_pri.TbUserprojCurveDatum).filter(
            md_pri.TbUserprojCurveDatum.User_ID == user_id and
            md_pri.TbUserprojCurveDatum.Proj_ID == proj_id).delete()
        data = md_pri.TbUserprojCurveDatum(User_ID=user_id,
                                           Proj_ID=proj_id,
                                           Curve_Data=curve_data)
        session_pri.add(data)
        session_pri.commit()
        print('添加数据成功')
        session_pri.close()
        return True

    except:
        print('添加数据失败')
        return False


# 返回存储计算表格数据恢复表
def return_userproj_curve_data(user_id, proj_id):
    try:
        data = session_pri.query(
            md_pri.TbUserprojCurveDatum.Curve_Data
        ).filter(
            md_pri.TbUserprojCurveDatum.User_ID == user_id,
            md_pri.TbUserprojCurveDatum.Proj_ID == proj_id
        ).all()
        if data:
            data=data[0]
        return data
    except:
        print(traceback.print_exc())
        return False


if __name__ == '__main__':
    jsondata = {"tabs1": [
        {"User_ID": "1",
         "Proj_ID": "9",
         "DvpUnit_Name": "测试1",
         "Year": "2020",
         "StimType_Name": "类别1",
         "Stim_WellNumber_Valid": 99999999,
         "Stim_WellValidRatio": 1,
         "Stim_ValidPeriod_Average": 1,
         "Stim_IncreOil_Average": 1,
         "StimOil_CommodityRatio": 1,
         "Oil_Price": 1,
         "OilSale_TotalTaxRatio": 1,
         "Money_Discount": 1,
         "Oil_OprCost": 1,
         "StimCost_Average": 22,
         "Status": 0,
         "Remark": "str"}],
        "tabs2": [{"User_ID": "1",
                   "Proj_ID": "9",
                   "DvpUnit_Name": "测试1",
                   "Year": "2020",
                   "StimType_Name": "类别1",
                   "Stim_WellNumber_Valid": 99999999,
                   "Stim_WellValidRatio": 1,
                   "Stim_ValidPeriod_Average": 1,
                   "Stim_IncreOil_Average": 1,
                   "StimOil_CommodityRatio": 1,
                   "Oil_Price": 1,
                   "OilSale_TotalTaxRatio": 1,
                   "Money_Discount": 1,
                   "Oil_OprCost": 0,
                   "StimCost_Average": 22,
                   "Remark": "str",
                   "StimOil_WellOprCost_Average": 1,
                   "StimOil_Total_Tax_Average": 0.0001,
                   "StimOil_Gross_SalesIncome_Average": 0.01,
                   "StimOil_Static_Gains_Average": -22.990099999999998,
                   "StimOil_NPV_Average": -22.980199,
                   "StimOil_ROI": -104.50045454545453,
                   "StimOil_NPVR": -104.45545,
                   "StimOil_FIRR": 2321.997778002222,
                   "Stim_IncreOil_Critical_Average": -22.444422000022445,
                   "StimOil_Price_Critical": 2345.6790123456785}],
        "page": {"total": 4, "pageIndex": 1, "pageSize": 12},
        "imput": {"unitname": ["测试6", "测试1", "测试2", "测试3"],
                  "years": ["2019", "2020", "2021"],
                  "method": ["类别5", "类1", "类别1", "类别3", "类别4", "类别2"]
                  },
        "toaltaxratio": [
            {"key": "测试12020",
             "value": {"DvpUnit_Name": "测试1",
                       "Year": "2020",
                       "OilSale_TotalTaxRatio": 66,
                       "AddedValue_TaxRatio": 66,
                       "Resource_TaxRatio": 66,
                       "EduAdd_TaxRatio": 66,
                       "CityDvpAdd_TaxRate": 66,
                       "Income_TaxRate": 66,
                       "ManageCost_Ratio": 66,
                       "Remark": "没有"}
             }],
        "oilcost": [
            {"key": "测试12020", "value": {"DvpUnit_Name": "测试1",
                                         "Year": "2020",
                                         "Oil_OprCost": 353,
                                         "FuelCost": 32,
                                         "Power_Cost": 32,
                                         "FluidDispose_Cost": 32,
                                         "OilDisplaceAgent_Cost": 68,
                                         "NGLRecovery_Cost": 86,
                                         "Transp_Cost": 68,
                                         "WellTest_Cost": 86,
                                         "Other_Cost": 86,
                                         "Depre_Cost": 86,
                                         "Remark": "无"}
             }
        ]
    }

    save_userproj_curve_data(user_id=1, proj_id=2, curve_data=jsondata)
